#include "feeditem.h"
#include "ui_feeditem.h"
#include "mytcpsocket.h"

FeedItem::FeedItem(struct st_feed _feed,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FeedItem)
{
    ui->setupUi(this);

    feed = _feed;
    QString head_url = QString("%1/%2.png").arg(PATH_IMG).arg(QString::number(feed.announcer));
    int borderWidth = 8; // 边框的宽度
    cv::Scalar borderColor(0, 0, 0, 255);
    QPixmap head = RoundBorderPixmap(downLoadFromUrl(head_url),borderWidth,borderColor);
    ui->lab_head->setPixmap(head);
    ui->lab_nick->setText(feed.nick);
    ui->lab_time->setText(feed.sendtime);

    if(feed.pyqtype == 95){
        ui->stackedWidget->setCurrentWidget(ui->page_95);
        ui->lab_content_95->setText(feed.content);
    }else if(feed.pyqtype == 96){
         ui->stackedWidget->setCurrentWidget(ui->page_96);
        // 解析内容和图片资源路径
        QStringList ml = feed.content.split('#');
        QString text = ml.front();
        QString url = ml.back();
        ui->lab_content_96->setText(text);
        ui->lab_content_96_img->setPixmap(downLoadFromUrl(url));
    }


    // 窗口渲染
    setQssStyle(QString("../style/FeedItem.qss"),this);



}

FeedItem::~FeedItem()
{
    delete ui;
}

void FeedItem::addPL(const QString& time,const QString nick,const QString &info)
{
    QString tmp = QString("%1\n%2:\n    %3").arg(time).arg(nick).arg(info);
    QLabel* label = new QLabel(tmp);
    label->setStyleSheet("font-size:12pt;border-top:1px solid dodgerblue;");
    label->setWordWrap(true);   // 换行
    label->setAlignment(Qt::AlignLeft | Qt::AlignTop);
    vec_pl.push_back(label);
}

void FeedItem::addDZ(const QString nick)
{
    QLabel* label = new QLabel(nick);
    label->setStyleSheet("color:#FF7D40");
    label->setAlignment(Qt::AlignLeft); // 左对齐
    label->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    label->adjustSize();
    ui->horizontalLayout->addWidget(label);

    vec_dz.push_back(label);
}

void FeedItem::removeDZ(const QString nick)
{
    QLabel* label;
    for(int i = 0;i<vec_dz.size();i++){
        if(vec_dz[i]->text() == nick)
            label = vec_dz[i];
    }
    ui->horizontalLayout->removeWidget(label);
    // 取消红心
    setUNDZ();
    // 从点赞列表中删除
    vec_dz.removeOne(label);
    // 释放资源
    delete label;
}

void FeedItem::on_btn_like_clicked()
{
    QVariantMap map;
    // 点赞
    if(ui->btn_like->isChecked() == true){

        map["cmd"] = "like_feed";
        map["uid"] = GlobalObject::getGlobalObject()->getUserInfo().sdata.dd_num;
        map["nick"] = GlobalObject::getGlobalObject()->getUserInfo().sdata.nick;
        map["feedID"] = feed.feedID;
        map["announcer"] = feed.announcer;
        map["content"] = "like";

    }
    // 取消点赞
    else{
        map["cmd"] = "unlike_feed";
        map["uid"] = GlobalObject::getGlobalObject()->getUserInfo().sdata.dd_num;
        map["nick"] = GlobalObject::getGlobalObject()->getUserInfo().sdata.nick;
        map["feedID"] = feed.feedID;
        map["announcer"] = feed.announcer;
        map["content"] = "unlike";

    }
    //发送报文
    GlobalObject::getGlobalObject()->sendRequest(map);

}

void FeedItem::on_btn_exchange_clicked()
{
    QVariant var;
    var.setValue(vec_pl);
    emit sig_plwindow(var);
}

st_feed FeedItem::getFeed() const
{
    return feed;
}


void FeedItem::on_btn_pl_clicked()
{
    // 获取用户输入的评论内容
    QString info = ui->le_exchange->text();

    if(info.isEmpty())
       return;

    // 封装评论动态
    QVariantMap map;
    map["cmd"] = "pl_feed";
    map["uid"] = GlobalObject::getGlobalObject()->getUserInfo().sdata.dd_num;
    map["nick"] = GlobalObject::getGlobalObject()->getUserInfo().sdata.nick;
    map["feedID"] = feed.feedID;
    map["announcer"] = feed.announcer;
    map["content"] = info;

    //发送报文
    GlobalObject::getGlobalObject()->sendRequest(map);

    ui->le_exchange->clear();
}

QVector<QLabel *> FeedItem::getVec_dz() const
{
    return vec_dz;
}

void FeedItem::setDZed()
{
    ui->btn_like->setChecked(true);
}

void FeedItem::setUNDZ()
{
    ui->btn_like->setChecked(false);
}

